<%-- 
    Document   : managing_products
    Created on : Mar 25, 2012, 10:42:27 PM
    Author     : Ervin  James
--%>

<%@page import="ContentManagers.RoleManagers.PMManager"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="DBConnection.ConnectionFactory"%>
<%@page import="ContentManagers.RoleManagers.AuthenticationManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>

<% //employ temporary session management here
            try{
            boolean check = false;
        
            check = new AuthenticationManager().inSuchDB(3, request, response);
            
            if(check == true)
            {
        %>
<html>
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         <link rel="stylesheet" type="text/css" href="css/managing_int.css" />
         <script type='text/javascript' src='Javascript/managerjs.js'></script>
        <title>JSP Page</title>
    </head>
    <body>
        
         <%@ include file="managing_header.jsp"%>
                <% //Call admin header file. The header contains the links for other pages. %>


                <div id ="body_wrapper">
        
                        
                <div id="product_manager_body" class="content_box">
                    
            <h1>PRODUCT MANAGER!</h1>
            <form name ="addProduct" method="post" onsubmit="return validateAddProduct()" action="../ServletAddProduct">
            <br> Add Product!<br>
            <br>Product name:
            <br><input class="inputbox" type="text" value="" name="name" autocomplete ="off" onfocus="clearInitial(this, 'Username')"/>
            <br>Description:
            <br><input class="inputbox" type="text" value="" name="description" autocomplete ="off" onfocus="clearInitial(this, 'Password')"/>
            <br>Price:
            <br><input class="inputbox" type="text" value="" name="price" autocomplete ="off" onfocus="clearInitial(this, 'Password')"/>
            <br><input type="submit" name="Submit" value="Submit"/>
            <br>
            <%
             if(session.getAttribute("message")!=null) {
             
             if(session.getAttribute("message").equals("1"))
                out.println("\nInvalid product name.\n");
             else if(session.getAttribute("message").equals("2"))
                out.println("\nInvalid product description.\n");
             else if(session.getAttribute("message").equals("3"))
                out.println("\nInvalid price value.\n");
             else if (session.getAttribute("message").equals("4"))
                out.println("\nError in adding product. Please check database connection\n");
             else if (session.getAttribute("message").equals("5"))
                out.println("\nProduct already exists in the inventory.\n");
             else if (session.getAttribute("message").equals("6"))
                out.println("\nProduct is successfully added to the inventory.\n");
             
             session.removeAttribute("message");
             }
            %>
            </form>
</div>


                    
                    
                    
                    
                <%
                out.println("List of Products<br><br>");


                ResultSet rs = new PMManager().getProductList(Integer.parseInt(session.getAttribute("managerType").toString()));
                //check if no valid row / move forward
                if(rs == null || rs.first() == false){
                    out.println("No products of this type.<br><br>");
                }
                else {
                    //restart from before first row
                    rs.beforeFirst();

                    int indexNo = 0;
                    ArrayList indexer = new ArrayList();

                    while(rs.next())
                    {
                    indexer.add(indexNo, rs.getString("product_id"));
                    %>
                    <div class="manageProduct">
                        <form name ="expiredPassword" method="post" onsubmit="" action="../ServletManageProduct">
                            <input type="text" name="productIndex" value="<%out.println(indexNo);%>" hidden/>
                            <b>Product Name:</b><input type="text" name="product_name" value="<%out.println(rs.getString("product_name"));%>"/>
                            <br><b>Product Description:</b><input type="text" name="description" value="<%out.println(rs.getString("description"));%>"/>
                            <br><b>Price:</b> <input type="text" name="price" value="<%out.println(rs.getString("price"));%>"/>
                            <br><input type="submit" id="submit" name="SubmitManage" value="Update"/>
                            <input type="submit" id="submit" name="SubmitManage" value="Delete"/>
                            <%
                        if((session.getAttribute("errorMessage"))!=null)
                        {
                            int formNo = Integer.parseInt(session.getAttribute("formNo").toString());
                            if((session.getAttribute("errorMessage"))== "1" && formNo==indexNo)
                                out.println("Invalid Product Name.");
                            else if((session.getAttribute("errorMessage"))== "2" && formNo==indexNo)
                                out.println("Invalid Product Description.");
                            else if((session.getAttribute("errorMessage"))== "3" && formNo==indexNo)
                                out.println("Invalid Price.");
                            else if((session.getAttribute("errorMessage"))== "4" && formNo==indexNo)
                                out.println("Edit successful.");
                            else if((session.getAttribute("errorMessage"))== "5" && formNo==indexNo)
                                out.println("A database error has occured.");
                            else if((session.getAttribute("errorMessage"))== "6" && formNo==indexNo)
                                out.println("Another product already exists with the same name.");

                            session.removeAttribute("errorMessage");
                        }
                            %>
                        </form>
                        </div>
                        <br>
                    <%

                    indexNo++;
                    }
                    //pass the array using session
                    request.getSession().setAttribute("indexer", indexer);

                }
                %>
                </div>

                </div>
                
         <%@ include file="managing_footer.jsp" %>
         <% //Call default footer file %>
        
    </body>
</html>

<%}
             
             else
                 new AuthenticationManager().redirectToProperIndex(3, request, response);
                
                         
            
    }catch(Exception e)
        {   session = request.getSession();
            session.setAttribute("username", "xxxxdefaultxxxx");
            session.setAttribute("sessionID", "xxxxdefaultxxxx");
        
            response.sendRedirect("../index.jsp");}
            
%>